package tw.asap.plus.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;
import tw.asap.plus.entity.response.OrderCreated;
import tw.asap.plus.entity.response.ShopIndex;
import tw.asap.plus.ui.activity.OrderDetailActivity;
import tw.asap.plus.utils.T;

/**
 * Created by Yax on 15/11/9.
 */
public class OrderAdapter extends RecyclerView.Adapter {

    Context context;
    RecyclerView recyclerView;
    public ArrayList<OrderCreated> list;

    private LayoutInflater inflater;

    String[] orderForm = new String[]{"等待商家确认",
            "已支付,等待商家确认",
            "已确认,制作中",
            "配送中",
            "订单已完成",
            "商家取消订单",
            "商家取消订单",
            "商家取消订单",};

    public OrderAdapter(Context context, RecyclerView recyclerView, ArrayList<OrderCreated> list) {
        this.context = context;
        this.recyclerView = recyclerView;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.item_order, null);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.setItemClickListener(() -> {
            Intent intent = new Intent(context, OrderDetailActivity.class);
            if (viewHolder.getAdapterPosition() == RecyclerView.NO_POSITION) {
                return;
            }
            intent.putExtra("ORDER_SN", list.get(viewHolder.getAdapterPosition()).getOrder_sn());
            context.startActivity(intent);
        });
        view.setOnClickListener(viewHolder);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ViewHolder) holder).setData(position);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @InjectView(R.id.merchant_name)
        TextView merchantName;
        @InjectView(R.id.order_status)
        TextView orderStatus;
        @InjectView(R.id.order_img)
        SimpleDraweeView orderImg;
        @InjectView(R.id.order_amt)
        TextView orderAmt;
        @InjectView(R.id.order_date)
        TextView orderDate;
        @InjectView(R.id.delivery_info)
        TextView deliveryInfo;

        ItemClickListener itemClickListener = null;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }

        public ViewHolder(View itemView, ItemClickListener listener) {
            super(itemView);
            ButterKnife.inject(this, itemView);
            this.itemClickListener = listener;
        }

        public void setData(int position) {
            merchantName.setText(list.get(position).getShopname());
            orderStatus.setText(orderForm[Integer.parseInt(list.get(position).getOrder_status())]);
            orderImg.setImageURI(Uri.parse(list.get(position).getLogo()));
            orderAmt.setText("$".concat(getFormatAmt(list.get(position).getPay_amount())));
            orderDate.setText(list.get(position).getCreate_time());
        }

        public void setItemClickListener(ItemClickListener mListener) {
            this.itemClickListener = mListener;
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onClick();
            }
        }
    }


    public interface ItemClickListener {
        void onClick();
    }

    public String getFormatAmt(String amt) {
        String formatAmt = "";
        try {
            formatAmt = String.format("%d", Integer.parseInt(amt) / 100);
        } catch (Exception e) {
            T.e(e.toString());
        }
        return formatAmt;
    }
}
